#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	while (pq->head != pq->tail)
	{
		QueuePop(pq);
	}

}

void QueuePop(Queue* pq)
{
	assert(pq);

	assert(pq->head);
	if (pq->size != 1)
	{
		QNode* tmp = pq->head;
		pq->head = pq->head->next;
		free(tmp);
		tmp = NULL;

	}
	else
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	pq->size--;

}
void QueuePush(Queue* pq, QDateType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed !");
		exit(1);
	}

	newnode->val = x;
	newnode->next = NULL;

	if (pq->size == 0)
	{
		pq->head= pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

QDateType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->head->val;
}
QDateType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->tail);

	return pq->tail->val;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	
	return pq->size == 0;
}

int QueueSize(Queue* pq)
{
	return pq->size;
}

void QueuePrint(Queue* pq)
{
	while (pq->size != 0)
	{
		int tmp = QueueFront(pq);
		printf("%d ", tmp);
		QueuePop(pq);
	}
	printf("\n");
} 